#pragma once

#include <SimpleDef.h>
#include <Synchronization/SpinLock.h>
;
/* FAT32 BPB 扇区 */
#pragma pack(push, 1)
typedef struct __FAT32BPBHEADER
{
    BYTE BS_jmpBoot[3]; /* 0 ~ 2 */
    BYTE BS_OEMName[8]; /* 3 ~ 10 */
    WORD BPB_BytsPerSec; /* 11 ~ 12 */
    BYTE BPB_SecPerClus; /* 13 */
    WORD BPB_ResvdSecCnt; /* 14 ~ 15 */
    BYTE BPB_NumFATs; /* 16 */
    WORD BPB_RootEntCnt; /* 17 ~ 18 */
    WORD TotSec16; /* 19 ~ 20 */
    BYTE BPB_Media; /* 21 */
    WORD BPB_FATSz16; /* 22 ~ 23 */
    WORD BPB_SecPerTrk; /* 24 ~ 25 */
    WORD BPB_NumHeads; /* 26 ~ 27 */
    DWORD BPB_HiddSec; /* 28 ~ 31 */
    DWORD BPB_TotSec32; /* 32 ~ 35 */
    /* FAT32 EBPB */
    DWORD BPB_FATSz32; /* 36 ~ 39 */
    WORD BPB_ExtFlags; /* 40 ~ 41 */
    WORD BPB_FSVer; /* 42 ~ 43 */
    DWORD BPB_RootClus; /* 44 ~ 47 */
    WORD BPB_FSInfo; /* 48 ~ 49 */
    WORD BPB_BkBootSec; /* 50 ~ 51 */
    BYTE BPB_Reserved[12]; /* 52 ~ 63 */
    BYTE BS_DrvNum; /* 64 */
    BYTE BS_Reserved1; /* 65 */
    BYTE BS_BootSig; /* 66 */
    DWORD BS_VolID; /* 67 ~ 70 */
    BYTE BS_VolLab[11]; /* 71 ~ 81 */
    BYTE BS_FilSysType[8]; /* 82 ~ 89 */
    BYTE MBR_Code [420];
    BYTE BPB_Magic[2]; /* 0x55 0xAA */
} FAT32BPBHEADER, *LPFAT32BPBHEADER;

typedef DWORD FAT32ENTRY;
typedef FAT32ENTRY *LPFAT32ENTRY;

#define FAT32_RESERVED (0x0FFFFFF8)
#define FAT32_EOC (0xFFFFFFFF)

typedef struct __FAT32DIR
{
    BYTE DIR_Name[11];
    BYTE DIR_Attr;
    BYTE DIR_NTRes;
    BYTE DIR_CrtTimeTenth;
    WORD DIR_CrtTime;
    WORD DIR_CrtDate;
    WORD DIR_LstAccDate;
    WORD DIR_FstClusHI;
    WORD DIR_WrtTime;
    WORD DIR_WrtDate;
    WORD DIR_FstClusLO;
    DWORD DIR_FileSize;
} FAT32DIR, *LPFAT32DIR;

typedef struct __FileControlBlock
{
    DWORD dwFileType; /* 文件类型 */
    LPSTR lpszPath; /* 文件路径 */
    DWORD dwFirstCluster; /* 文件首簇的簇号 */
    DWORD dwFirstSector; /* 文件首簇的首扇区号 */
    DWORD dwFileSize; /* 文件大小 */
    SpinLock kFileLock; /* 文件锁 */
    DWORD dwReferences; /* 引用计数 */
    struct __FileControlBlock* lpNext;
} FileControlBlock, *LPFileControlBlock;

#define FILE_READ_ONLY (0x01)
#define FILE_HIDDEN (0X02)
#define FILE_SYSTEM (0x04)
#define FILE_VOLUME_ID (0x08)
#define FILE_DIRECTORY (0x10)
#define FILE_ARCHIVE (0x20)
#define FILE_LONG_NAME (FILE_READ_ONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_VOLUME_ID)

extern FileControlBlock g_FCBCache; /* FCB 列表，头结点为根目录 */
extern FAT32BPBHEADER g_BPB; /* 引导盘 BPB */

#pragma pack(pop)